﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotonJXC.Util;
using BotonJXC.Entity;
using System.Data;
using NHibernate;
using BotonJXC.DAO;

namespace BotonJXC.BLL
{
    public class OrdersBLL:BaseBLL<OrdersDAO,Orders,int>
    {
        public OrdersBLL()
        { 
        }
        /// <summary>
        /// 保存订单
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public bool saveOrders(Orders orders ) {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                base.Dao.Save(orders);
                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;

            }
            finally
            {
                HBHelper.Close();
            }
        }

        /// <summary>
        /// 更新订单
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public bool updateOrders(Orders orders ,Acceptance Acceptance)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                base.Dao.Update(orders);             
                    //自动验收单
                AcceptanceBll AcceptanceBll = new AcceptanceBll();
                AcceptanceBll.Save(Acceptance);
           
                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                return false;
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public bool updateOrders(Orders orders, OutStocks OutStocks)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                //自动生成出库单
                OutStocksBLL OutStocksBLL = new OutStocksBLL();
                OutStocksBLL.Save(OutStocks);
                orders.OutStocks = OutStocks;
                base.Dao.Update(orders);             
                
              
                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                return false;
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public bool updateOrders(Orders orders,Orders _orders)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
              
                base.Dao.Update(orders);
                base.Save(_orders);
                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                return false;
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public bool updateOrders(Orders orders)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                base.Dao.Update(orders);
                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                return false;
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public bool deleteOrders(Orders orders)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                base.Dao.Delete(orders);
                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                return false;
            }
            finally
            {
                HBHelper.Close();
            }
        }
        /// <summary>
        /// 条件查询订单
        /// </summary>
        /// <param name="begintime"></param>
        /// <param name="endtime"></param>
        /// <param name="orders"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public PageModel<Orders> findOrdersByCondition(DateTime? begintime, DateTime? endtime, Orders orders, int start, int count)
        {
            return base.Dao.findOrdersByCondition(begintime, endtime, orders, start, count);
        }

        /// <summary>
        /// 查询全部订单
        /// </summary>
        /// <returns></returns>
        public IList<Orders> findAllOrders() {
           return  base.FindAll();
        }

        /// <summary>
        /// 根据ID查询订单
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public Orders findOrdersById(string orderid) {
            return base.Get(Int32.Parse(orderid));
        }

        /// <summary>
        /// 根据订单编号查询订单
        /// </summary>
        /// <param name="orderSn"></param>
        /// <returns></returns>
        public Orders findOrdersBySn(string orderSn)
        {

            IList<Orders> list = base.Dao.findOrdersBySn(orderSn);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }  
        /// <summary>
        /// 根据订单编号查询订单啊才能
        /// </summary>
        /// <param name="orderSn"></param>
        /// <returns></returns>
        public Orders findOrdersBySn(string orderSn, string orderType)
        {

            IList<Orders> list = base.Dao.findOrdersBySn(orderSn, orderType);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }
    }
}
